package cn.itcast.service.impl;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.mapper.SetmealMapper;
import cn.itcast.pojo.Setmeal;
import cn.itcast.pojo.vo.SetmealVO;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper mapper;

    @Transactional
    public void add(List<Integer> ids, Setmeal sm) {
        //新增套餐
        mapper.add(sm);
        //向套餐中加入检查组
        if(ids!=null && ids.size()>0){

            for(int gid : ids) {

                mapper.setSetMealAndCheckGroup(sm.getId(),gid);

            }
        }
    }



    public PageResult findPage(QueryPageBean bean) {

        //pageHelper.starpage(当前页码，每页展示条数)
        PageHelper.startPage(bean.getCurrentPage(),bean.getPageSize());

        //将查寻出的结果封装为集合对象
        List<Setmeal> list = mapper.findPage(bean.getQueryString());

        //将集合载入到pageInfo中，反推出总记录数
        PageInfo pi = new PageInfo(list);

        //将总页数与当前页集合载入到PageResult进行返回
        return  new PageResult(pi.getTotal(),list);

    }



    public List<Setmeal> findAll() {

        return mapper.findAll();
    }



    public Setmeal findById(int id) {

        return mapper.findById(id);
    }



    public Map<String, Object> getSetmealReport() {

        Map<String, Object> map = mapper.getSetmealReport();

        Map hash = new HashMap();
        hash.put("setmealNames",map.keySet());
        hash.put("setmealCount",map.values());

        return hash;
    }
}
